/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carrancao.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Lubnnia
 */
public class CriptografiaUtil {

    private static MessageDigest md = null;

    /**
      * Metodo estatico para a geracao do algoritmo de criptografia.
    */

    static {
	try {
            md = MessageDigest.getInstance("MD5");
	} catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
	}
    }

    /**
      * Criptografa a senha.
      * @param pwd String A senha normal.
      * @return String A senha criptografada.
    */

    public static String criptografar(String pwd) {
	if (md != null) {
            return new String(hexCodes(md.digest(pwd.getBytes())));
	}
            return null;
	}

    private static char[] hexCodes(byte[] text) {
	char[] hexOutput = new char[text.length * 2];
	String hexString;
	for (int i = 0; i < text.length; i++) {
            hexString = "00" + Integer.toHexString(text[i]);
	    hexString.getChars(hexString.length() - 2, hexString.length(), hexOutput, i * 2);
	}
	return hexOutput;
    }

}
